package com.wanghaoran.mapper;

import com.wanghaoran.dto.DeptPageDTO;
import com.wanghaoran.dto.RolePageDTO;
import com.wanghaoran.entity.Dept;
import com.wanghaoran.entity.EmpRole;
import com.wanghaoran.entity.Role;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author wanghaoran
 */
@Repository
public interface RoleMapper {

    @Insert("""
        insert into ums_role
        (`title`, `info`, `version`, `deleted`, `created`, `updated`)
        values 
        (#{title}, #{info}, #{version}, #{deleted}, #{created}, #{updated})
        """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Role role);


    @Select("""
            select * from ums_role t
            where t.id = #{param1} and t.deleted = 0
            """)
    Role select(Long id);


    @Select("""
            <script>
            select * from ums_role t
            <where>
                <if test='title != null'> title like concat('%', #{title}, '%') and </if>
                t.deleted = 0
            </where>
            </script>
            """)
    List<Role> list(RolePageDTO dto);

    @Update("""
            <script>
            update ums_role
            <set>
               <if test = 'title != null'> title = #{title}, </if>
               <if test = 'info != null'>info = #{info},</if>
               <if test = 'version != null'>version = #{version},</if>
               <if test = 'deleted != null'>deleted = #{deleted},</if>
               <if test = 'created != null'>created = #{created},</if>
               <if test = 'updated != null'>updated = #{updated},</if>
               version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version}
            </script>
            """)
    int update(Role role);

    @Update("""
            update ums_role set deleted = 1, updated = current_timestamp where id = #{id}
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_role set deleted = 1, updated = current_timestamp where 
            <foreach collection="list" item="e" open="id in (" close=")" separator=",">
                ${e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Select("""
            select * from ums_role t1
            where id in (select t2.fk_role_id from ums_emp_role t2
                         where t2.fk_emp_id = #{param1} and t2.deleted = 0)
            and t1.deleted = 0;
            """)
    List<Role> listByEmpId(Long empId);



    @Update("""
            update ums_emp_role set deleted = 1, updated = current_timestamp
            where fk_emp_id = #{param1}
            """)
    int deleteEmpRoleByEmpId(Long empId);

    @Insert("""
            <script>
            insert into ums_emp_role 
            (fk_emp_id, fk_role_id, version, deleted, created, updated) 
            values
            <foreach collection='list' item='e' separator=','>
                (#{e.fkEmpId}, #{e.fkRoleId}, #{e.version}, #{e.deleted}, #{e.created}, 
                #{e.updated})
            </foreach>
            </script>
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertEmpRoleBatch(List<EmpRole> empRoles);
}
